package cc.togetherit.jcrawler.down;

import org.apache.log4j.Logger;

import cc.togetherit.jcrawler.dataPool.CrawlerPool;
import cc.togetherit.jcrawler.fetch.FetchRequest;
import cc.togetherit.jcrawler.util.StrUtil;

public abstract class AbstractDownEngine implements DownEngine, Runnable {
    boolean isstop = false;
    private DownEngineConfig config;
    public CrawlerPool<String> downPool;
    public CrawlerPool<FetchRequest> fetchPool;
    private String domain;
    Logger logger = Logger.getLogger(getClass());

    public abstract String download(String pageUrl);

    public void start() {
        String url;
        while (!isstop) {
            url = downPool.consume();
            if (StrUtil.isNull(url)) {

                url = downPool.consume();
            }
            if (StrUtil.isNull(url)) {
                isstop = true;
                logger.error("downEngine will stop,cause this task finished!!!");
                break;
            }
            logger.debug("download url : "+url);
            String content = download(url);
            FetchRequest request = new FetchRequest(this.getDomain(), url, content);
            fetchPool.produce(request);
        }
        stop();

    }

    public void run() {
        start();

    }



    public DownEngineConfig getConfig() {
        return config;
    }

    public void setConfig(DownEngineConfig config) {
        this.config = config;
    }

    public CrawlerPool<String> getDownPool() {
        return downPool;
    }

    public void setDownPool(CrawlerPool<String> downPool) {
        this.downPool = downPool;
    }

    public CrawlerPool<FetchRequest> getFetchPool() {
        return fetchPool;
    }

    public void setFetchPool(CrawlerPool<FetchRequest> fetchPool) {
        this.fetchPool = fetchPool;
    }

    public boolean isIsstop() {
        return isstop;
    }

    public void setIsstop(boolean isstop) {
        this.isstop = isstop;
    }



    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public AbstractDownEngine(CrawlerPool<String> downPool, CrawlerPool<FetchRequest> fetchPool) {
        super();
        this.downPool = downPool;
        this.fetchPool = fetchPool;
    }



}
